package com.yf1706.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yf1706.bean.Dep;
import com.yf1706.bean.Emp;
import com.yf1706.bean.EmpPositionChangeRecord;
import com.yf1706.bean.PostSetting;
import com.yf1706.bean.RankSetting;
import com.yf1706.bean.User;
import com.yf1706.service.ApproveService;
import com.yf1706.service.DepService;
import com.yf1706.service.EmpService;
import com.yf1706.service.SettingService;
import com.yf1706.service.UserService;
import com.yf1706.vo.BaseSalaryVO;
import com.yf1706.vo.Information;
import com.yf1706.vo.UserEmpDepVO;

@Controller
@RequestMapping("personnelChange")
public class PersonnelChangeController {
	@Autowired
	private EmpService es;
	@Autowired
	private ApproveService as;
	@Autowired
	private DepService ds;
	@Autowired
	private SettingService ss;
	@Autowired
	private UserService us;
	
	//到职位变动申请列表页面
	@RequestMapping("positionChangeRecordList")
	public ModelAndView positionChangeRecordList() {
		System.out.println("进入positionChangeRecordList");
		ModelAndView mv=new ModelAndView("zxy/position_change_record_list");
		return mv;
	}
	
	//增加员工职务变动记录
	@RequestMapping("editPositionChange")
	@ResponseBody
	public Information editPositionChange(HttpServletRequest request) {
		System.out.println("进入editPositionChange");
		int recordId=Integer.parseInt(request.getParameter("recordId"));
		EmpPositionChangeRecord pcr=as.findEPCR(recordId);
		int newDep=0;
		if(!request.getParameter("newDep").equals("")) {
			newDep=Integer.parseInt(request.getParameter("newDep"));
			Dep dep=ds.findByDepId(newDep);
			pcr.setNewDep(dep.getDepName());
		}
		int newPost=0;
		if(!request.getParameter("newPost").equals("")) {
			newPost=Integer.parseInt(request.getParameter("newPost"));
			PostSetting post=ss.findPostSettingByPostId(newPost);
			pcr.setNewPost(post.getPostName());
		}
		int newRank=0;
		if(!request.getParameter("newRank").equals("")) {
			newRank=Integer.parseInt(request.getParameter("newRank"));
			RankSetting rank=ss.findRanktSettingByRankId(newRank);
			pcr.setNewRank(rank.getRankName());
		}
		int approverId=0;
		if(!request.getParameter("approver").equals("")) {
			approverId=Integer.parseInt(request.getParameter("approver"));
			UserEmpDepVO ued=us.fundUserBasicByUserId(approverId);
			pcr.setApproverId(approverId);
			pcr.setApprover(ued.getUserName());
			pcr.setApproveDepId(ued.getDepId());
			pcr.setApproveDep(ued.getDepName());
		}
		
		String approverDep=request.getParameter("approverDep");
		String docNum=request.getParameter("docNum");
		String remark=request.getParameter("remark");
		
		pcr.setDocNum(docNum);
		pcr.setRemark(remark);
		System.out.println(pcr);
		as.addPositionChangeRecord(pcr);
		return Information.SUCCESS;
	}
	
	//到职位变动申请详情页面
	@RequestMapping("positionChangeRecordInfo")
	public ModelAndView positionChangeRecordInfo(int recordId) {
		System.out.println("进入positionChangeRecordInfo");
		ModelAndView mv=new ModelAndView("zxy/position_change_record_info");
		EmpPositionChangeRecord pcr=as.findEPCR(recordId);
		mv.addObject("pcr", pcr);
		return mv;
	}
	
	
	//到添加职位变动申请页面
	@RequestMapping("positionChange")
	public ModelAndView positionChange(String empId) {
		ModelAndView mv=new ModelAndView("zxy/position_change_add");
		Emp emp=es.queryById(Integer.parseInt(empId));
		mv.addObject("emp", emp);
		String empStr=JSON.toJSONString(emp);
		mv.addObject("empStr", empStr);
		return mv;
	}
	
	//分页查询所有职位变动申请记录
	@RequestMapping("findEPCRByPage")
	@ResponseBody
	public Map<String, Object> findEPCRByPage(int page,int rows) {
		System.out.println("进入findEPCRByPage");
		System.out.println(page+":"+rows);
		Page<EmpPositionChangeRecord> pages=PageHelper.startPage(page, rows);
		List<EmpPositionChangeRecord> list=as.queryAllEPCR();
		Long total= pages.getTotal();
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	//分页查询所有职位变动申请记录
	@RequestMapping("queryEPCRBySearch")
	@ResponseBody
	public Map<String, Object> queryEPCRBySearch(int page,int rows,String searchValue) {
		System.out.println("进入findEPCRByPage");
		System.out.println(page+":"+rows+":"+searchValue);
		Page<EmpPositionChangeRecord> pages=PageHelper.startPage(page, rows);
		List<EmpPositionChangeRecord> list=as.queryBySearch(searchValue);
		Long total= pages.getTotal();
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	

	//审批通过职位变动申请
	@RequestMapping("rejectPositionChangeRecord")
	@ResponseBody
	public Information rejectPositionChangeRecord(int recordId,String approveComments) {
		System.out.println("进入rejectPositionChangeRecord");
		System.out.println(recordId+":"+approveComments);
		EmpPositionChangeRecord pcr=as.findEPCR(recordId);
		pcr.setApproveStatus((byte)3);
		pcr.setApproveComments(approveComments);
		as.addPositionChangeRecord(pcr);
		return Information.SUCCESS;
		
	}
	//审批通过职位变动申请
	@RequestMapping("approvePositionChangeRecord")
	@ResponseBody
	public Information approvePositionChangeRecord(int recordId,String approveComments) {
		System.out.println("进入approvePositionChangeRecord");
		System.out.println(recordId+":"+approveComments);
		EmpPositionChangeRecord pcr=as.findEPCR(recordId);
		Emp emp=es.findBasicByEmpId(pcr.getEmpId());
		System.out.println(emp.getPostSetting().getPostName()+emp.getRankSetting().getRankName()+emp.getDep().getDepName());
		if(pcr.getNewDep()!=null) {
			System.out.println(pcr.getNewDep());
			emp.setDep(ds.findByDepName(pcr.getNewDep()));
		}
		if(pcr.getNewPost()!=null) {
			System.out.println(pcr.getNewPost());
			emp.setPostSetting(ss.findPostSettingByPostName(pcr.getNewPost()));
		}
		if(pcr.getNewRank()!=null) {
			System.out.println(pcr.getNewRank());
			emp.setRankSetting(ss.findRanktSettingByRankName(pcr.getNewRank()));
		}
		System.out.println(emp.getPostSetting().getPostName()+emp.getRankSetting().getRankName()+emp.getDep().getDepName());
		es.saveEmpAllById(emp);
		pcr.setApproveDate(new Date());
		pcr.setChangeDate(new Date());
		pcr.setApproveStatus((byte)2);
		pcr.setApproveComments(approveComments);
		as.addPositionChangeRecord(pcr);
		return Information.SUCCESS;
	}
	
	//取消职位变动申请
	@RequestMapping("cancelPersitionChange")
	@ResponseBody
	public Information cancelPersitionChange(HttpServletRequest request) {
		System.out.println("进入cancelPersitionChange");
		int recordId=Integer.parseInt(request.getParameter("recordId"));
		EmpPositionChangeRecord pcr=as.findEPCR(recordId);
		pcr.setApproveStatus((byte)4);
		as.addPositionChangeRecord(pcr);
		return Information.SUCCESS;
	}
	
	//删除职位变动申请
	@RequestMapping("deletePersitionChangeByRecordId")
	@ResponseBody
	public Information deletePersitionChangeByRecordId(HttpServletRequest request) {
		System.out.println("进入deletePersitionChangeByRecordId");
		System.out.println("id_arr" + request.getParameter("id_arr"));
		JSONArray arr = JSONArray.parseArray(request.getParameter("id_arr"));
		for (Object obj : arr) {
			int recordId = (int) obj;
			as.deleteEPCR(recordId);
		}
		return Information.SUCCESS;
	}
}
